<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="源IP" prop="sourceIp">
        <el-input
          v-model="queryParams.sourceIp"
          placeholder="请输入源IP"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="源端口" prop="sourcePort">
        <el-input
          v-model="queryParams.sourcePort"
          placeholder="请输入源端口"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="目的IP" prop="destinationIp">
        <el-input
          v-model="queryParams.destinationIp"
          placeholder="请输入目的IP"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="目的端口" prop="destinationPort">
        <el-input
          v-model="queryParams.destinationPort"
          placeholder="请输入目的端口"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="协议" prop="protocol">
<!--        <el-input
          v-model="queryParams.protocol"
          placeholder="请输入协议"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />-->
        <el-select
          v-model="queryParams.protocol"
          placeholder="请输入协议"
          clearable
          class="!w-240px"
        >
          <el-option key="S7COMM" label="S7COMM" value="S7COMM" />
          <el-option key="DNP3" label="DNP3" value="DNP3" />
          <el-option key="MODBUS" label="MODBUS" value="MODBUS" />
        </el-select>
      </el-form-item>
      <el-form-item label="功能码" prop="functionCode">
        <el-input
          v-model="queryParams.functionCode"
          placeholder="请输入功能码"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="handleLearn()"
          v-hasPermi="['security:whitelist-event:create']"
        >
          <Icon icon="ep:video-play" class="mr-5px" /> {{learnBtnText}}
        </el-button>
        <el-button
          v-if="startBtnEnable"
          type="warning"
          plain
          @click="handleStart()"
          v-hasPermi="['security:whitelist-event:create']"
        >
          <Icon icon="ep:video-play" class="mr-5px" /> 开启告警
        </el-button>
        <el-button
          v-if="stopBtnEnable"
          type="danger"
          plain
          @click="handleStop()"
          v-hasPermi="['security:whitelist-event:create']"
        >
          <Icon icon="ep:video-pause" class="mr-5px" /> 关闭告警
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['security:whitelist-event:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="源IP" align="center" prop="sourceIp" width="120"/>
      <el-table-column label="源端口" align="center" prop="sourcePort" />
      <el-table-column label="目的IP" align="center" prop="destinationIp" width="120"/>
      <el-table-column label="目的端口" align="center" prop="destinationPort" />
      <el-table-column label="协议" align="center" prop="protocol" />
      <el-table-column
        label="发生时间"
        align="center"
        prop="eventTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="功能码" align="center" prop="functionCode" />
      <el-table-column label="功能码中文" align="center" prop="functionCodeCn" />
      <el-table-column label="消息类型" align="center" prop="msgType" />
      <el-table-column label="寄存器区" align="center" prop="registerArea" />
      <el-table-column label="DB区" align="center" prop="dbArea" />
      <el-table-column label="点类型" align="center" prop="pointType" />
      <el-table-column label="对象组号" align="center" prop="objectGroupNum" />
      <el-table-column label="变体号" align="center" prop="variantNum" />
<!--      <el-table-column label="状态（0正常 1停用）" align="center" prop="status" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />-->
<!--      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['security:whitelist-event:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['security:whitelist-event:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>-->
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <WhitelistEventForm ref="formRef" @success="getList" />
  <WhitelistOperateForm ref="operateFormRef" @success="initPageData" />
</template>

<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { WhitelistEventApi, WhitelistEventVO } from '@/api/security/whitelistevent'
import { WhitelistOperateApi, WhitelistOperateVO } from '@/api/security/whitelistoperate'
import WhitelistEventForm from './WhitelistEventForm.vue'
import WhitelistOperateForm from '@/views/security/whitelistoperate/WhitelistOperateForm.vue'
import {DICT_TYPE, getIntDictOptions} from "@/utils/dict";

/** 白名单事件 列表 */
defineOptions({ name: 'WhitelistEvent' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<WhitelistEventVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const latestOperate = ref<WhitelistOperateVO>({}) // 详情
const learnBtnText = ref('开始学习') // 列表的总页数
const startBtnEnable = ref(false) // 列表的总页数
const stopBtnEnable = ref(false) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  sourceIp: undefined,
  sourcePort: undefined,
  destinationIp: undefined,
  destinationPort: undefined,
  protocol: undefined,
  eventTime: [],
  functionCode: undefined,
  functionCodeCn: undefined,
  msgType: undefined,
  registerArea: undefined,
  dbArea: undefined,
  pointType: undefined,
  objectGroupNum: undefined,
  variantNum: undefined,
  status: undefined,
  remark: undefined,
  createTime: [],
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await WhitelistEventApi.getWhitelistEventPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

const getLatestOperate = async () => {
  const data = await WhitelistOperateApi.getLatestWhitelistOperate()
  latestOperate.value = data
  if (data && data.id && data.learning) {
    learnBtnText.value = '学习中'
  } else if(data && data.id && !data.learning) {
    learnBtnText.value = '重新学习'
    if (data.status === 0) {
      startBtnEnable.value = false
      stopBtnEnable.value = true
    } else {
      startBtnEnable.value = true
      stopBtnEnable.value = false
    }
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await WhitelistEventApi.deleteWhitelistEvent(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

const operateFormRef = ref()
const openOperateForm = (type: string, id?: number) => {
  operateFormRef.value.open(type, id)
}

const handleLearn = async () => {
  try {
    if (latestOperate && latestOperate.value && latestOperate.value.id && latestOperate.value.learning) {
      message.alert('白名单自学习中……')
      return;
    }
    // 二次确认
    await message.confirm('是否确认?')
    // 发起
    openOperateForm('create')
  } catch {}
}

const handleStart = async () => {
  try {
    // 二次确认
    await message.confirm('是否确认?')
    // 发起
    const data = {} as WhitelistOperateVO
    await WhitelistOperateApi.startWhitelistOperate(data)
    message.success('成功')
    // 刷新列表
    await initPageData()
  } catch {}
}

const handleStop = async () => {
  try {
    // 二次确认
    await message.confirm('是否确认?')
    // 发起
    const data = {} as WhitelistOperateVO
    await WhitelistOperateApi.stopWhitelistOperate(data)
    message.success('成功')
    // 刷新列表
    await initPageData()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await WhitelistEventApi.exportWhitelistEvent(queryParams)
    download.excel(data, '白名单事件.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

const initPageData = () => {
  getList()
  getLatestOperate()
 }

/** 初始化 **/
onMounted(() => {
  initPageData()
})
</script>
